<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/25 0025
 * Time: 15:05
 */

namespace app\storage\controller;
use app\storage\model\SeatMachineModel;
use cmf\controller\AdminBaseController;
use app\storage\model\MachineModel;
use app\storage\model\StorageModel;
use think\request;
use app\storage\service\CommonService;
use think\Db;

use service\logger\LoggerFactory;


class MachineController extends AdminBaseController
{
    use \app\storage\util\Common;
    private $commonService;
    private $machineModel;
    private $storageModel;
    private $seatMachineModel;
    public function __construct(CommonService $commonService,MachineModel $machineModel,StorageModel $storageModel,seatMachineModel $seatMachineModel,Request $request = null)
    {
        parent::__construct($request);
        $this->machineModel = $machineModel;
        $this->commonService = $commonService;
        $this->storageModel = $storageModel;
        $this->seatMachineModel = $seatMachineModel;
    }
    /**
     * 机器视图
     */
    public function index()
    {
        $storageUuid = input('storageUuid');
        $storageInfo = $this->commonService->get_assign_storage($storageUuid);
        $assign['storageUuid'] = $storageUuid;
        $assign['storageInfo'] = $storageInfo;
        $this->assign($assign);
        return $this->fetch('index');
    }

    public function machineSeatUsed()
    {
        $storageUuid = input('storageUuid');
        $storageInfo = $this->commonService->get_assign_storage($storageUuid);
        $assign['storageUuid'] = $storageUuid;
        $assign['storageInfo'] = $storageInfo;
        $this->assign($assign);
        return $this->fetch('machineSeatUsed');
    }

    /***
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 添加机台视图
     */
    public function add()
    {
        $storageList = $this->storageModel->getStorageList();
        $assign['storageUuid'] = $storageList['uuid'];
        $assign['storageInfo'] = $storageList;
        $this->assign($assign);
        return $this->fetch('add');
    }

    /***
     * @return \think\response\Json
     * 获取机台信息展示
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMachine()
    {
        $where = $this->getWhere();
       $result = $this->machineModel->getMachine($where);
        return json($result);
    }

    /***
     * @return \think\response\Json
     * 获取正在使用的机台信息展示
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMachineByStatus()
    {
        $where = $this->getWhere();
        $result = $this->machineModel->getMachineByStatus($where);
        return json($result);
    }

    /***
     * 确认MachineId是否重复
     */
    public function checkMachineId(){
        $machineId = input('machineId');
        $uuid = input('uuid');
        $where['machine_Id'] = $machineId;
        $where['storage_uuid'] = $uuid;
        $data = $this->machineModel->findMachine($where);//如果数据库已有该machine_Id的记录，则返回false
        if(!empty($data)){
            $result = 1;
        }
        else{
            $result = 0;
        }
        ajax_return((bool)$result);
    }
    /***
     *添加机台以及机台上的站位
     ***/
    public function addMachine(){
        $data = input();//获取添加机台页面的信息
        Db::startTrans();//开启事务
        try{
            $this->machineModel->addMachine($data);//添加机台
            $where = ['machine_id'=>$data['machineId']];
            $machineData = $this->machineModel->findMachine($where);//根据添加机台页面输入的machineId找到新生成的机器的记录
            $machineData['seatNumber'] = $data['seatNumber'];//获取刚刚生成的机台的uuid
            $this->seatMachineModel->addMachineSeat($machineData);//为该机台添加站位
            Db::commit();//提交
        }catch (\Exception $e){
            //如果运行过程中出现错误，则回滚
            Db::rollback();
        }

        $this->redirect('/storage/machine/index');//重新跳转到机台展示页面
    }

    /***
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取机台的站位
     */
    public function getMachineSeat(){
        $condition = $this->getWhere();
        $machineUuid = input('machineUuid');
        $where['machine_uuid'] = $machineUuid;
        $condition['where'] =$where;
        $result = $this->seatMachineModel->getSeat($condition);
        return json($result);
    }

    /***
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取正在使用的机台的站位
     */
    public function getMachineSeatByIsUsed(){
        $condition = $this->getWhere();
        $machineUuid = input('machineUuid');
        $where['machine_uuid'] = $machineUuid;
        $where['is_used']=1;
        $condition['where'] =$where;
        $result = $this->seatMachineModel->getMachineSeatByIsUsed($condition);
        return json($result);
    }


    /***
     *
     * 删除某个机台以及上面的所有站位
     */
    public function deleteMachine(){
        $machineUuid = input('uuid');
        $whereSeat['machine_uuid'] = $machineUuid;
        $whereMachine['uuid'] = $machineUuid;
        Db::startTrans();//开启事务
        try{
            $this->seatMachineModel->deleteSeat($whereSeat);//删除该机台上的站位
            $this->machineModel->deleteMachine($whereMachine);//删除机台
            Db::commit();//提交修改
        }catch (\Exception $e){
            //发生错误回滚
            Db::rollback();
        }
        $this->redirect('/storage/machine/index');

    }

}